#!/usr/bin/env ruby

# Add project classes
require 'DirHandler'
require 'ContextHandler'
require 'DatabaseHandler'
require 'ExifHandler'
require 'constants'
require 'GeonamesHandler'
require 'WeatherHandler'
require 'CategorizationHandler'

# Program constants
# Show or not show debug messages
DEBUG = 1

################################################################################

def debug(msg)
  if (DEBUG)
    puts "DEBUG: " + msg.to_s  
  end
end

################################################################################

debug("-------------------------------------------------------------------")
debug("Started application - Listening for applied picture directories...")
debug("-------------------------------------------------------------------")

# database handler
db = DatabaseHandler.new
#connect handler
db.connect

# file and directory handler
dirh = DirHandler.new

# exif information handler
exifh = ExifHandler.new

# geonames data handler
geo = GeonamesHandler.new

# weather context handler
weah = WeatherHandler.new

# categorization handler
ch = CategorizationHandler.new

# statistics
total_files = 0

################################ CYCLE #############################

while true
    
  # fetch directory
  path = db.get_path
  
  if(path != "")
    # check if directory
    if (!dirh.new_dir(path))
      debug(dirh.directory + " is not a directory")
      # reset dir table in DB
      db.path_processed  
      next
    end
    
    debug("STATUS: #{Time.now} directory found: #{path}")
    debug("-------------------------------------------------------------------")
    
    # jpg files in directory
    image_files = dirh.get_files
    
    # copy to local storate
    dirh.copy_images_to_storage
    
    # cycle through folder and add images to DB
    added_files = 0
    
    image_files.each do |file|
      # output info to console
      debug("Processing file: " + file)
      added_files += 1
      total_files += 1
      
      debug("EXIF process")
      exif_info = exifh.get_exif(file)
      
      debug("Adding image to DB")
      res = db.add_image(exif_info)
      
      debug("Geo information process")
      res = geo.process_image(file)
      
      debug("Weather data process")
      res = weah.process_weather(file)
      
      debug("Panoramio tags process")
      res = ch.panoramio_tags(file)
      
      debug("Image categorizational process")
      res = ch.process_search(file)
      
      # output info to console 
      debug("Added #{file} to database and image storage")
      debug("-------------------------------------------------------------------")
    end
    
    debug("Status: #{Time.now} Added a total of #{added_files} to the DB.")
    debug("Status: #{Time.now} Total of #{total_files} in the DB.")
    debug("-------------------------------------------------------------------")
    
    # reset dir table in DB
    db.path_processed
    
    # change CWD back to standard
    Dir.chdir(SELF_DIR)

    #debug(Dir.getwd)  
  end
  
  # sleep for 10 seconds
  sleep 10

end

############################## END CYCLE ###########################

#close db handler
db.close

debug("end")